<template>
  <div class="video-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <h1>视频上传与展示</h1>
        </div>
      </template>

      <!-- 上传视频 -->
      <div class="upload-section">
        <el-upload
          action="http://localhost:8088/videos/upload"
          :on-change="handleFileUpload"
          :auto-upload="false"
          :show-file-list="false"
          accept="video/*"
        >
          <template #trigger>
            <el-button type="primary">选择视频文件</el-button>
          </template>
          <el-button type="success" @click="uploadVideo">上传视频</el-button>
        </el-upload>
        <p v-if="uploadMessage" class="upload-message">{{ uploadMessage }}</p>
      </div>

      <!-- 视频列表 -->
      <div class="video-list">
        <h2>视频列表</h2>
        <el-table :data="videos.content" style="width: 100%" border stripe @row-click="selectVideo">
          <el-table-column prop="name" label="视频名称" />
          <el-table-column prop="uploadTime" label="上传时间">
            <template #default="{ row }">
              {{ new Date(row.uploadTime).toLocaleString() }}
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[5, 10, 20, 50]"
          :total="videos.totalElements"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

      <!-- 播放视频 -->
      <div v-if="selectedVideo" class="video-player">
        <h2>播放视频</h2>
        <video :src="getVideoUrl(selectedVideo.filePath)" controls class="small-video"></video>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from '../axios';
import { ElMessage } from 'element-plus';

const file = ref(null);
const videos = ref({
  content: [], // 当前页的视频数据
  totalElements: 0, // 总数据量
  size: 10, // 每页大小
  number: 1, // 当前页码（从 1 开始）
});
const selectedVideo = ref(null);
const uploadMessage = ref('');

// 分页相关状态
const currentPage = ref(1); // 当前页码（从 1 开始）
const pageSize = ref(10); // 每页大小

// 获取视频列表
const fetchVideos = async (page = 1, size = 5) => {
  try {
    const response = await axios.get('/videos', {
      params: { page, size },
    });
    videos.value = response.data;
  } catch (error) {
    ElMessage.error('获取视频列表失败');
  }
};

// 上传视频
const handleFileUpload = (uploadFile) => {
  file.value = uploadFile.raw;
};

const uploadVideo = async () => {
  if (!file.value) {
    ElMessage.warning('请先选择视频文件');
    return;
  }

  const formData = new FormData();
  formData.append('file', file.value);

  try {
    const response = await axios.post('/videos/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
    ElMessage.success('视频上传成功');
    fetchVideos(currentPage.value - 1, pageSize.value); // 刷新当前页的视频列表
  } catch (error) {
    ElMessage.error('视频上传失败');
  }
};

// 选择视频
const selectVideo = (video) => {
  selectedVideo.value = video;
};

// 获取视频 URL
const getVideoUrl = (filePath) => {
  return `http://localhost:8088${filePath}`;
};

// 处理每页大小变化
const handleSizeChange = (size) => {
  pageSize.value = size;
  fetchVideos(1, size); // 每页大小变化时，加载第一页数据
};

// 处理页码变化
const handleCurrentChange = (page) => {
  currentPage.value = page;
  fetchVideos(page, pageSize.value); // 加载对应页的数据
};

// 初始化时加载第一页数据
onMounted(() => {
  fetchVideos();
});
</script>

<style scoped>
.video-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 20px;
}

.card-header {
  text-align: center;
}

.upload-section {
  margin-bottom: 20px;

}

.upload-message {
  margin-top: 10px;
  color: #67c23a;
}

.video-list {
  margin-bottom: 20px;
}

.video-player {
  text-align: center;
  margin-top: 20px;
}

video {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.small-video {
  width: 600px;
  height: 300px; /* 设置更小的高度 */
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
::v-deep.el-table th,
::v-deep.el-table td {
  height: 40px;
  line-height: 40px;
  text-align: center;
}
</style>